package com.ruizhong.centercontroll.controller;

import com.ruizhong.centercontroll.entity.TPrebuildComponent;
import com.ruizhong.centercontroll.service.TPrebuildComponentService;
import com.ruizhong.centercontroll.util.Result;
import com.ruizhong.centercontroll.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.bind.JAXBException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/prebuildComponent/api")
@CrossOrigin
public class TPrebuildComponentController {

    @Autowired
    private TPrebuildComponentService tPrebuildComponentService;


    @GetMapping("/getPrebuildComponentByType/{type}")
    public Result getPrebuildComponentByType(@PathVariable(value = "type") Integer type){
        List prebuildComponents = tPrebuildComponentService.getPrebuildComponentByType(type);
        return new Result(ResultCode.success,null,prebuildComponents);
    }

    @PostMapping("/saveAsFormat")
    public Result saveAsFormat(@RequestBody Map<String,Object> tPrebuildComponentMap){
        tPrebuildComponentService.savaAsFormat(tPrebuildComponentMap);

        return new Result(ResultCode.success,"保存成功",null);
    }

    @DeleteMapping("/deleteComponentFormat/{id}")
    public Result deleteComponentFormat(@PathVariable(value = "id") Integer id){
        tPrebuildComponentService.deleteComponentFormat(id);
        return new Result(ResultCode.success,"删除成功",null);
    }

    @PostMapping("/ExportComponentTemplate")
    public ResponseEntity<InputStreamResource> ExportComponentTemplate(@RequestBody Map<String,Object> tPrebuildComponentMap) throws JAXBException {
        return tPrebuildComponentService.exportComponentTemplate(tPrebuildComponentMap);
    }

    @PostMapping("/ImportComponentTemplate")
    public Result ImportComponentTemplate(@RequestBody MultipartFile file){
        tPrebuildComponentService.ImportComponentTemplate(file);
        return new Result(ResultCode.success,"导入成功",null);
    }
}
